package com.matt.repository.impl;

import com.matt.foundation.query.ChildrenCourseQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.ChildrenExtendInfoCourseRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/8
 * *******************************
 */
@Component
public class ChildrenExtendInfoCourseRepositoryImpl extends BaseNativeSqlRepository implements ChildrenExtendInfoCourseRepository {


    @Override
    public List<Object[]> findChildrenCoursePage(ChildrenCourseQuery query) {
        String querySql = String.format("select c.id, c.name, c.birthday, i.earlyTotalCourseNum , i.earlyLeftCourseNum , cc.totalCourseSize, cc.overCourseSize, cc.evaluateCourseSize, cc.leaveCourseSize, cc.skipCourseSize, cc.id as childrenCourseId " +
                " from children c LEFT join children_extend_info i on c.id = i.childrenId " +
                " LEFT join children_course cc on c.id = cc.childrenId WHERE true and i.earlyTotalCourseNum > -1 ");

        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and c.aid='%s' ", query.getAreaId());
        }

        if (Objects.nonNull(query.getStatus()) && query.getStatus() == 0) {
            querySql += String.format(" and i.earlyLeftCourseNum > 0 ");
        }

        if (Objects.nonNull(query.getStatus()) && query.getStatus() == 1) {
            querySql += String.format(" and i.earlyLeftCourseNum = 0 ");
        }
        if (StringUtils.isNotBlank(query.getChildrenName())) {
            querySql += String.format(" and c.name like concat('%s', '%s', '%s') ", "%", query.getChildrenName(), "%");
        }

        querySql += " order by c.createTime desc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());
        List<Object[]> objectArrayList = sqlArrayList(querySql);
        return objectArrayList;
    }

    @Override
    public Long countChildrenCourse(ChildrenCourseQuery query) {
        String querySql = String.format("select count(c.id) " +
                        " from children c LEFT join children_extend_info i on c.id = i.childrenId " +
                        " LEFT join children_course cc on c.id = cc.childrenId WHERE true and i.earlyTotalCourseNum > -1");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and c.aid='%s' ", query.getAreaId());
        }
        if (Objects.nonNull(query.getStatus()) && query.getStatus() == 0) {
            querySql += String.format(" and i.earlyLeftCourseNum > 0 ");
        }
        if (Objects.nonNull(query.getStatus()) && query.getStatus() == 1) {
            querySql += String.format(" and i.earlyLeftCourseNum = 0 ");
        }
        if (StringUtils.isNotBlank(query.getChildrenName())) {
            querySql += String.format(" and c.name like concat('%s', '%s', '%s') ", "%", query.getChildrenName(), "%");
        }

        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
